package com.cos.lession72.slice;

import com.cos.lession72.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.components.TextFilter;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

import java.util.Arrays;

public class LoginAbilitySlice extends AbilitySlice {
    TextField name;
    TextField pwd;
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_login);
        //获取2个输入框组件对象
        name = (TextField) findComponentById(ResourceTable.Id_input_name);
        pwd = (TextField) findComponentById(ResourceTable.Id_input_pwd);
        //调用onFocusChange方法
        name.setFocusChangedListener(this::onFocusChange);
        pwd.setFocusChangedListener(this::onFocusChange);

    }

    /**
     *
     * @param component
     *      触发事件的对象 ==>js jquery click(function(){this})
     *      那个组件触发了事件,component就是触发事件的组件对象
     * @param b
     */
    public void onFocusChange(Component component, boolean b) {
        //获取2个
        //失焦时进入方法
        if (!b) {
            TextField t = (TextField) component;
            String value = t.getText();
            //判断字符串长度
            //默认提示为正确提示
            String msg = "ok";
            //默认颜色为正确的绿色
            Color color = Color.GREEN;
            //创建设置背景对象
            ShapeElement element = new ShapeElement();
            /* 示例获取背景颜色 */
//            RgbColor[] rgbColors = element.getRgbColors();
            /* 示例获取角度 */
//            float cornerRadius = element.getCornerRadius();
            /* 示例获取边框颜色 */
//            RgbColor strokeColor = element.getStrokeColor();
//            ToastDialog td = new ToastDialog(getContext());
//            td.setText(Arrays.toString(rgbColors)+"--"+cornerRadius+"--"+strokeColor);
            //居中显示
//            td.setAlignment(LayoutAlignment.CENTER);
//            td.setDuration(5000);
//            td.show();

            //设置圆角
            element.setCornerRadius(30);
            //设置边框 为绿
            element.setStroke(3,new RgbColor(0,255,0));
            if (value.trim().length()<6) {
                //字符串长度不符合 重新设置提示信息 和提示文字颜色
                msg="不能少于6位";
                color=Color.RED;
                /* 设置边框 为红 */
                element.setStroke(3,new RgbColor(255,0,0));
            }

            //判断给那个输入框组件的提示Text组件进行信息设置
            Text text;
            //是name输入框触发的
            if (component==name) {
                text = (Text) findComponentById(ResourceTable.Id_input_name_msg);
                //把背景加入到当前的输入框组件当中
                name.setBackground(element);
            } else {
                //pwd 输入框触发的
                text = (Text) findComponentById(ResourceTable.Id_input_pwd_msg);
                //把背景加入到当前的输入框组件当中
                pwd.setBackground(element);
            }
            //设置提示信息和文本颜色
            text.setText(msg);
            text.setTextColor(color);
        }

    }
}
